package com.diyunapp.xilinapp.me;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.bean.SharedBean;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.diyunapp.xilinapp.utils.AbDateUtil;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShraedDetilActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.money_recorder_list)
    ListView moneyRecorderList;
    @Bind(R.id.notice_refresh_view)
    SwipeRefreshLayout noticeRefreshView;
    private SharedAdapter sharedAdapter;
    private List shares;
    private LayoutInflater inflater;
    private String id;
    private List<SharedBean.DatasBean.DataBean> info = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shraed_detil);
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        sharedAdapter = new SharedAdapter(this);
        ButterKnife.bind(this);
        View headView;
        headView = findViewById(R.id.sharedetil_menubar_inc);
        super.MenuWidget(headView);
        id = getIntent().getExtras().getString("id");
        toast(id);
        initShared();
        moneyRecorderList.setAdapter(sharedAdapter);
        noticeRefreshView.setColorSchemeColors(R.color.domain_orange, R.color.green,
                R.color.blue, R.color.yellow);

        noticeRefreshView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zhangxiaoxidebug", "myordersSwiperefreshlayout onRefresh...");
                initShared();
            }
        });

    }

    @OnClick(R.id.go_back)
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.go_back:
                finish();
                break;
        }

    }
    public void initShared() {
        String url = ""+MainActivity.url+"act=goods&op=my_tj&member_id="+ MainActivity.myLogin.getMemberid()+"";

        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(10000);
        http.send(HttpRequest.HttpMethod.POST, url,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        noticeRefreshView.setRefreshing(false);
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo=" + resultinfo);
//                        Logger.e("=================================="+resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
                            int result = resultObject.optInt("code");

                            if (result == 200) {
                                info.clear();
                                JSONObject information = resultObject.optJSONObject("datas");
                                Log.i("zhangxiaoxidebug", "information=" + information);
                                JSONArray listArray = information.optJSONArray("data");
                                Log.i("zhangxiaoxidebug", "listArray=" + listArray);
                                for (int i = 0; i < listArray.length(); i++) {
                                    JSONObject recordObject = listArray.optJSONObject(i);
                                    String member_id=recordObject.optString("member_id");
                                    String member_name = recordObject.optString("member_name");
                                    long member_time = recordObject.optLong("member_time");
                                    int yizhifu_tj_amount = recordObject.optInt("yizhifu_tj_amount");
                                    int complete_tj_amount = recordObject.optInt("complete_tj_amount");


                                    info.add(new SharedBean.DatasBean.DataBean(member_id,member_name, member_time, yizhifu_tj_amount, complete_tj_amount));
                                }
                                sharedAdapter.setList(info);
                                sharedAdapter.notifyDataSetChanged();

                            } else {
                                toast("当前暂无数据");
                                noticeRefreshView.setVisibility(View.GONE);

                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            toast("查询失败");
                            e.printStackTrace();
                            noticeRefreshView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        noticeRefreshView.setRefreshing(false);
                        toast("请求失败");
                        noticeRefreshView.setVisibility(View.GONE);
                    }
                });
    }

    class SharedAdapter extends BaseAdapter {
        private List<SharedBean.DatasBean.DataBean> info = new ArrayList<SharedBean.DatasBean.DataBean>();
        private Context context;

        public SharedAdapter(Context context) {
            this.context = context;

        }

        public void setList(List<SharedBean.DatasBean.DataBean> list) {
            this.info = list;
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return info == null ? 0 : info.size();
        }

        @Override
        public Object getItem(int position) {
            return info == null ? null : info.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_shared, null);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            SharedBean.DatasBean.DataBean bean=info.get(position);
            viewHolder.huiyuanname.setText(bean.getMember_name());
            viewHolder.happTime.setText(AbDateUtil.getStringByFormat(bean.getMember_time()*1000,"yyyy-MM-dd"));
            viewHolder.shopMoney.setText(bean.getYizhifu_tj_amount()+"");
            viewHolder.starMoney.setText(bean.getComplete_tj_amount()+"");
            viewHolder.imageView3.setVisibility(View.GONE);
            return convertView;
        }



        class ViewHolder {
            @Bind(R.id.huiyuanname)
            TextView huiyuanname;
            @Bind(R.id.imageView3)
            ImageView imageView3;
            @Bind(R.id.happ_time)
            TextView happTime;
            @Bind(R.id.shop_money)
            TextView shopMoney;
            @Bind(R.id.star_money)
            TextView starMoney;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}
